"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : autocommit
Case Name   :  非兼容B库不支持设置autocommit为off
Create At   : 2023/01/16
@zou_jialiang0501162244
Description :
    1、创建用户，创建A、PG、C、B兼容模式数据库
    2、A兼容模式库中设置当前库和兼容B库关闭autocommit
    3、PG兼容模式库中设置当前库和兼容B库关闭autocommit
    4、C兼容模式库中设置当前库和兼容B库关闭autocommit
    5、清理环境
Expect      :
    1、成功
    2、合理报错
    3、合理报错
    4、合理报错
    5、成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestAutocommit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = {'A': 'db_autocommit_0001_1',
                        'PG': 'db_autocommit_0001_2',
                        'C': 'db_autocommit_0001_3',
                        'B': 'db_autocommit_0001_4'}
        self.u_name = 'u_autocommit_0001'
        self.err_msg = 'ERROR:  SET AUTOCOMMIT TO OFF is no longer supported'

    def test_autocommit(self):
        text1 = '-----step1:创建用户，创建A、PG、C、B兼容模式数据库;expect:成功-----'
        self.log.info(text1)
        create_user = f"drop user if exists {self.u_name} cascade;" \
                      f"create user {self.u_name} password 'test@123';"
        create_user_res = self.pri_sh.execut_db_sql(create_user)
        self.log.info(create_user_res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, create_user_res,
                      f'创建用户失败: {text1}')
        for mode, dbname in self.db_name.items():
            create_db = f"drop database if exists {dbname};" \
                        f"create database {dbname} dbcompatibility '{mode}';"
            create_db_res = self.pri_sh.execut_db_sql(create_db)
            self.log.info(create_db_res)
            self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_db_res,
                          f'创建数据库失败: {text1}')

        text2 = '-----step2:A兼容模式库中设置当前库和兼容B库关闭' \
                'autocommit;expect:合理报错-----'
        self.log.info(text2)
        sql = f"set autocommit to off;" \
              f"alter database {self.db_name['A']} set autocommit to off;" \
              f"alter database {self.db_name['B']} set autocommit to off;" \
              f"alter user {self.u_name} set autocommit to off;" \
              f"alter session set autocommit = off;"
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name['A'])
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.err_msg), 5, f'执行失败: {text2}')

        text3 = '-----step3:PG兼容模式库中设置当前库和兼容B库关闭' \
                'autocommit;expect:合理报错-----'
        self.log.info(text3)
        sql = f"set autocommit = 0;" \
              f"alter database {self.db_name['PG']} set autocommit = off;" \
              f"alter database {self.db_name['B']} set autocommit to 0;" \
              f"alter user {self.u_name} set autocommit = off;" \
              f"alter session set autocommit to off;"
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name['PG'])
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.err_msg), 5, f'执行失败: {text3}')

        text4 = '-----step4:C兼容模式库中设置当前库和兼容B库关闭' \
                'autocommit;expect:合理报错-----'
        self.log.info(text4)
        sql = f"set autocommit to false;" \
              f"alter database {self.db_name['C']} set autocommit to 0;" \
              f"alter database {self.db_name['B']} set autocommit to 0;" \
              f"alter user {self.u_name} set autocommit = false;" \
              f"alter session set autocommit to false;"
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name['C'])
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.err_msg), 5, f'执行失败: {text3}')

    def tearDown(self):
        text5 = '-----step5:清理环境; expect:成功-----'
        self.log.info(text5)
        drop_user_res = self.pri_sh.execut_db_sql(
            f"drop user if exists {self.u_name} cascade;")
        self.log.info(drop_user_res)
        result = []
        for dbname in self.db_name.values():
            create_db_res = self.pri_sh.execut_db_sql(
                f"drop database if exists {dbname};")
            self.log.info(create_db_res)
            result.append(create_db_res)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_user_res,
                      f'创建用户失败: {text5}')
        self.assertEqual(result.count(self.constant.DROP_DATABASE_SUCCESS), 4,
                         f'删库失败: {text5}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
